package com.ihelp.rest.utils;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.ihelp.rest.base.po.Page;

public class JsonCommon {

	/**
	 * 列表JSON 数据
	 * @param page
	 * @return
	 */
	public static JSONObject getGridJson(Page<?> page) {
		Map<String, Object> jsonMap = new Hashtable<String, Object>();
		jsonMap.put("total", page.getTotalCount());
		jsonMap.put("rows", page.getItems());
		JSONObject json = JSONObject.fromObject(jsonMap);
		return json;
	}
	
	/**
	 * 列表JSON 数据
	 * @param total
	 * @param rows
	 * @return
	 */
	public static JSONObject getGridJson(int total, List<?> rows) {
		Map<String, Object> jsonMap = new Hashtable<String, Object>();
		jsonMap.put("total", total);
		jsonMap.put("rows", rows);
		JSONObject json = JSONObject.fromObject(jsonMap);
		return json;
	}
	
	
	/**
	 * 把字符串按指定分隔符分割并转换成List
	 * @param ids
	 * @param split
	 * @return
	 */
	public static List<Object> idsTosList(String ids, String split) {
		List<Object> list = new ArrayList<Object>();
		String[] _IDS = ids.split(split);
		for (String id : _IDS) {
			list.add(id);
		}
		
		return list;
	}
}
